package com.ityun.ogapp.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.ityun.ogapp.dao.ContactRepository
import com.ityun.ogapp.data.ContactBean
import kotlinx.coroutines.flow.catch

class ContactModel(private val application: Application) : BaseViewModel() {
    private val contactRepository by lazy {
        ContactRepository(application)
    }
    val allContacts = MutableLiveData<List<ContactBean>>()
    fun loadContact() {
        launchOnViewModelScope {
            contactRepository.getAllContacts()
                .catch { allContacts.postValue(null) }
                .collect {
                    allContacts.postValue(it)
                }
        }
    }
}